﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoFile;
using VideoConfig;
using System.IO;
using IAlarm;
using System.Drawing;
using IConfig;

namespace AlarmManager
{
    public class FileAlarmReader : IAlarm.IAlarmReader
    {
        #region IAlarmReader 成员

        public GroupInfo[] ReadGroups(string area, string device, DateTime date)
        {
            string root = FileConfigReader.SingleConfig[area, device]["event"];
            if (root == string.Empty) return new GroupInfo[0] ;
            List<GroupInfo> groups = new List<GroupInfo>();
            //获取指定日期的报警组列表
            DirectoryInfo di = new DirectoryInfo(FilePath.GetDatePath(root, area, device, date));
            if (!di.Exists) return new GroupInfo[0];
            DirectoryInfo[] diGroups = di.GetDirectories();

            foreach (DirectoryInfo diGroup in diGroups)
            {
                FileInfo[] fiGroups = diGroup.GetFiles("*.group");
                if (fiGroups.Length == 0) continue;
                using (StreamReader srGroup = new StreamReader(fiGroups[0].FullName))
                {//读取所有报警信息
                    long groupID;
                    if (!long.TryParse(srGroup.ReadLine(), out groupID)) continue;
                    string groupArea = srGroup.ReadLine();
                    string groupDevice = srGroup.ReadLine();
                    short horizon;
                    if (!short.TryParse(srGroup.ReadLine(), out horizon)) continue;
                    short vertical;
                    if (!short.TryParse(srGroup.ReadLine(), out vertical)) continue;
                    int analyzeCount;
                    if (!int.TryParse(srGroup.ReadLine(), out analyzeCount)) continue;
                    int eventOperate;
                    if (!int.TryParse(srGroup.ReadLine(), out eventOperate)) continue;
                    string[] alarmIDs = srGroup.ReadToEnd().Split(new char[]{'\r','\n'}, StringSplitOptions.RemoveEmptyEntries);
                    GroupInfo group = new GroupInfo(groupID,groupArea,groupDevice,horizon,vertical,analyzeCount);

                    long alarmID;
                    //DateTime groupTime = new DateTime(groupID);
                    foreach (string id in alarmIDs)
                    {
                        if (!long.TryParse(id, out alarmID)) continue;
                        DateTime alarmTime = new DateTime(alarmID);
                        //获取指定报警信息路径
                        string alarmFile = FilePath.GetDateTimePathTimeFile(root, area, device, group.GroupTime, alarmTime, "alarm");
                        if (!File.Exists(alarmFile)) continue;

                        using (StreamReader srAlarm = new StreamReader(alarmFile))
                        {
                            long groupid;
                            if (!long.TryParse(srAlarm.ReadLine(), out groupid) || groupid != groupID) continue;
                            int frameIndex;
                            if (!int.TryParse(srAlarm.ReadLine(), out frameIndex)) continue;
                            long alarmid;
                            if (!long.TryParse(srAlarm.ReadLine(), out alarmid) || alarmid != alarmID) continue;
                            string[] regionAndType = srAlarm.ReadToEnd().Split('\n');
                            Rectangle[] regions = new Rectangle[regionAndType.Length];
                            int[] regionTypes = new int[regionAndType.Length];
                            for (int i = 0; i < regionAndType.Length; i++)
                            {
                                string[] XYWHT = regionAndType[i].Split(',');
                                if (XYWHT.Length != 5) continue;
                                int x, y, w, h, t;
                                if (!int.TryParse(XYWHT[0], out x)) continue;
                                if (!int.TryParse(XYWHT[1], out y)) continue;
                                if (!int.TryParse(XYWHT[2], out w)) continue;
                                if (!int.TryParse(XYWHT[3], out h)) continue;
                                if (!int.TryParse(XYWHT[4], out t)) continue;
                                regions[i] = new Rectangle(x, y, w, h);
                                regionTypes[i] = t;
                            }
                            AlarmInfo alarm = new AlarmInfo(groupid, frameIndex, alarmid, regions, regionTypes);

                            group.Add(alarm);
                        }
                    }

                    groups.Add(group);
                }
            }

            return groups.ToArray();
        }


        public Bitmap ReadFrame(GroupInfo group, AlarmInfo alarm)
        {
            string root = FileConfigReader.SingleConfig[group.Area, group.Device]["event"];
            if (root == string.Empty) return null;

            string alarmFile = FilePath.GetDateTimePathTimeFile(root, group.Area, group.Device, new DateTime(alarm.GroupID), alarm.AlarmTime, "jpg");

            if (!File.Exists(alarmFile)) return null;

            return new Bitmap(alarmFile);
        }

        #endregion
    }
}
